<?php

function reequipment_adminapi_createrelcycles($args)
{
    extract($args);

    // Argument check - make sure that all required arguments are present,
    // if not then set an appropriate error message and return
    $invalid = array();

    if (!isset($equipment_id) || !is_int($equipment_id)) {
        $invalid[] = 'equipment_id';
    } 
    if (count($invalid) > 0) {
        $msg = xarML('Invalid #(1)', join(', ', $invalid));
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    } 
    
    // Get the current cycle count
    $model = xarModAPIFunc('reequipment', 'user', 'getequipment', array('equipment_id' => $equipment_id));
    foreach ($model as $var){
    	$cycles = intval($var['cycles']);
    }
    
    // Assume that when this is entered is when it was ordered.
    $date_added = time();
   
    /* Fields in table.
    	maintenance_id
        item_id
        equipment_id
        cycles
        date_added
   */
   
    // Allow us to make a relationship on two item types.
    if (!isset($maintenance_id)){
   		$maintenance_id = '';
   	}
   	if (!isset($item_id)){
   		$item_id = '';
   	}
   	
    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['rel_cycles'];

    // Add item
    $query = "INSERT INTO $table ("
        . " maintenance_id, item_id, equipment_id, cycles, date_added)"
        . " VALUES (?, ?, ?, ?, ?)";
    $result =& $dbconn->Execute($query, array($maintenance_id, $item_id, $equipment_id, $cycles, $date_added));
    if (!$result) return;

    return true;
}
?>